<?php

namespace App\Http\Services;

use App\Models\OperateLog;
use Carbon\Carbon;

use AlibabaCloud\SDK\Dysmsapi\V20170525\Dysmsapi;

use Darabonba\OpenApi\Models\Config;
use AlibabaCloud\SDK\Dysmsapi\V20170525\Models\SendSmsRequest;

/**
 * Class OperateLogService
 * @package App\Http\Services
 */
class SendSmsService
{
    /**
     * OperateLogService constructor.
     */
    public function __construct()
    {

    }

    /**
     * 使用AK&SK初始化账号Client
     * @param string $accessKeyId
     * @param string $accessKeySecret
     * @return Dysmsapi Client
     */
    public static function createClient($accessKeyId, $accessKeySecret){
        $config = new Config([
            // 您的AccessKey ID
            "accessKeyId" => $accessKeyId,
            // 您的AccessKey Secret
            "accessKeySecret" => $accessKeySecret
        ]);
        // 访问的域名
        $config->endpoint = "dysmsapi.aliyuncs.com";
        return new Dysmsapi($config);
    }

    /**
     * @param string[] $args
     * @return void
     */
    public static function send($mobile, $content, $country_code=""){
        if(!$content || !$mobile) return "";

        $template_id =   'SMS_138076227'; //国内短信id
        if($country_code  && $country_code != 86){
            $template_id = 'SMS_138076646';
            $mobile = '00'.$country_code.$mobile;
        }
        $client = self::createClient(env('SMS_KEY_ID'), env('SMS_KEY_SECRET'));

        $sendSmsRequest = new SendSmsRequest([
            "phoneNumbers" => $mobile,
            "signName" => env("SMS_SIGN_NAME"),
            "templateCode" => $template_id,
            "templateParam" =>  json_encode([
                 "code"=> $content
            ])
        ]);

        // 复制代码运行请自行打印 API 的返回值
        return $client->sendSms($sendSmsRequest);
    }

}
